package com.vincent.kdp.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.kdp.R;
import com.vincent.kdp.adapter.CounterListExpandAdapter;
import com.vincent.kdp.adapter.InStockExpandAdapter;
import com.vincent.kdp.bean.RxBusPosBean;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.repository.CheckGoodsRepository;
import com.vincent.kdp.ui.bean.response.BaseOutput;
import com.vincent.kdp.ui.bean.response.CounterOutput;
import com.vincent.kdp.ui.bean.response.InStockOutput;
import com.vincent.kdp.util.RxBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

public class CounterGoodsListActivity extends AppCompatActivity {


    @Bind(R.id.counter_list_lv)
    ExpandableListView counterListLv;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.check_all)
    CheckBox checkAll;
    @Bind(R.id.list_down)
    TextView listDown;
    @Bind(R.id.list_md_text)
    TextView listMdText;

    private Subscription rxSbscription;
    private CounterListExpandAdapter adapter;
    private InStockOutput mbean;
    private String goodsIdList = "";
    private String OldId = "0";//当前门店
    private String mdId = "0";  //选择门店
    private CheckGoodsRepository checkGoodsRepository = new CheckGoodsRepository();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_counter_goods_list);
        ButterKnife.bind(this);
        OldId = getIntent().getStringExtra("id");
        String cname = getIntent().getStringExtra("name");
        System.out.println("Old====" + OldId);
        title.setText(cname+"商品列表");
        initView();
        getData();
        rxBusObservers();
    }

    private void initView() {
        adapter = new CounterListExpandAdapter(this);
        counterListLv.setAdapter(adapter);
        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    for (int i = 0; i < mbean.getData().size(); i++) {
                        mbean.getData().get(i).setGroupChecked("1");
                        for (int j = 0; j < mbean.getData().get(i).getGoodsList().size(); j++) {
                            mbean.getData().get(i).getGoodsList().get(j).setIsChecked("1");
                        }
                    }

                    adapter.notifyDataSetChanged();
                } else {
                    for (int i = 0; i < mbean.getData().size(); i++) {
                        mbean.getData().get(i).setGroupChecked("0");
                        for (int j = 0; j < mbean.getData().get(i).getGoodsList().size(); j++) {
                            mbean.getData().get(i).getGoodsList().get(j).setIsChecked("0");
                        }
                    }

                    adapter.notifyDataSetChanged();
                }
            }
        });


    }

    private void getIds() {
        for (int i = 0; i < mbean.getData().size(); i++) {
            for (int j = 0; j < mbean.getData().get(i).getGoodsList().size(); j++) {
                System.out.println("goodsIdList--j---" + goodsIdList);
                if (!"".equals(goodsIdList)) {

                    System.out.println("getStatus---" + mbean.getData().get(i).getGoodsList().get(j).getStatus());
                    System.out.println("getIsChecked---" + mbean.getData().get(i).getGoodsList().get(j).getIsChecked());
                    System.out.println("getInDepart---" + mbean.getData().get(i).getGoodsList().get(j).getInDepart());

                    if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 1 ||
                            Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 4) {
                        if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getIsChecked()) == 1

                                ) {
//
                            goodsIdList = goodsIdList + "," + mbean.getData().get(i).getGoodsList().get(j).getGoodsId();
                            System.out.println("goodsIdList-----" + goodsIdList);
                        }
                    }
                } else {
                    System.out.println("getStatus---" + mbean.getData().get(i).getGoodsList().get(j).getStatus());
                    System.out.println("getIsChecked---" + mbean.getData().get(i).getGoodsList().get(j).getIsChecked());
                    System.out.println("getInDepart---" + mbean.getData().get(i).getGoodsList().get(j).getInDepart());
                    if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 1 ||
                            Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getStatus()) == 4) {
                        if (Integer.parseInt(mbean.getData().get(i).getGoodsList().get(j).getIsChecked()) == 1

                                ) {
                            goodsIdList = mbean.getData().get(i).getGoodsList().get(j).getGoodsId();
                            System.out.println("goodsIdList--空---" + goodsIdList);
                        }
                    }
                }

            }
        }
    }


    private void rxBusObservers() {
        rxSbscription = RxBus.getInstance().toObserverable(RxBusPosBean.class)
                .subscribe(new Action1<RxBusPosBean>() {
                    @Override
                    public void call(RxBusPosBean bean) {
                        //T.showShort("11111111------"+String.valueOf(bean.getType()));
                        if (bean.getType() == 1) {

                            mdId = bean.getDepartId();
                            String name = bean.getName();
                            if (OldId.equals(mdId)) {
                                Toast.makeText(CounterGoodsListActivity.this, "当前是" + name + ",不能选择" + name + "转柜", Toast.LENGTH_SHORT).show();
                                mdId="0";
                            } else {
                                listMdText.setText(name);
                            }

                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        if (!rxSbscription.isUnsubscribed()) {
            rxSbscription.unsubscribe();
        }
        super.onDestroy();
    }

    @OnClick({R.id.back, R.id.list_md_choose, R.id.list_move, R.id.list_down})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.list_md_choose:
                checkGoodsRepository.getlistCounter(new RequestCallBack<CounterOutput>() {
                    @Override
                    public void onSuccess(CounterOutput bean) {

                        if (bean.getCode() == 0) {
                            Intent it = new Intent(CounterGoodsListActivity.this, ChooseDepartActivity.class);
                            Bundle b = new Bundle();
                            b.putSerializable("bean", bean);
                            it.putExtras(b);
                            startActivity(it);
                        } else {

                        }

                    }

                    @Override
                    public void onFalse(String msg) {
                        // dialog(msg);
                    }
                });
                break;
            case R.id.list_move:
                //转柜
                if ("0".equals(mdId)) {
                    Toast.makeText(this, "请选择柜台", Toast.LENGTH_SHORT).show();
                    return;
                }
                goodsIdList = "";
                getIds();
                System.out.println("goodsIdList--传---" + goodsIdList);
                checkGoodsRepository.moveCounter(goodsIdList, mdId, new RequestCallBack<BaseOutput>() {
                    @Override
                    public void onSuccess(BaseOutput bean) {

                        if (bean.getCode() == 0) {
                            if (bean.getCode() == 0) {
                                Toast.makeText(CounterGoodsListActivity.this, "转柜成功", Toast.LENGTH_SHORT).show();


                            } else {
                                Toast.makeText(CounterGoodsListActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } else {

                        }

                    }

                    @Override
                    public void onFalse(String msg) {
                        // dialog(msg);
                    }
                });


                break;
            case R.id.list_down:

                //下柜
//                if("0".equals(mdId)){
//                    Toast.makeText(this,"请选择门店",Toast.LENGTH_SHORT).show();
//                    return;
//                }
                goodsIdList = "";
                getIds();
                checkGoodsRepository.downCounter(goodsIdList, new RequestCallBack<BaseOutput>() {
                    @Override
                    public void onSuccess(BaseOutput bean) {

                        if (bean.getCode() == 0) {
                            if (bean.getCode() == 0) {
                                Toast.makeText(CounterGoodsListActivity.this, "下柜成功", Toast.LENGTH_SHORT).show();


                            } else {
                                Toast.makeText(CounterGoodsListActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } else {

                        }

                    }

                    @Override
                    public void onFalse(String msg) {
                        // dialog(msg);
                    }
                });
                break;
        }
    }

    public void getData() {
        checkGoodsRepository.getCounterList(new RequestCallBack<InStockOutput>() {
            @Override
            public void onSuccess(InStockOutput bean) {
                mbean = bean;
                adapter.setList(mbean.getData(), checkAll);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFalse(String msg) {
                // dialog(msg);
            }
        });
    }
}
